package com.coocaa.ops.admin.v1.panel.biz.impl;


import com.coocaa.ops.admin.v1.panel.biz.IPanelSubPanelRlsBiz;
import com.coocaa.ops.admin.v1.panel.mapper.IPanelSubPanelRlsMapper;
import com.coocaa.ops.common.core.base.BaseBizImpl;
import com.coocaa.ops.common.core.base.enums.ResourceStatusEnums;
import com.coocaa.ops.common.core.base.mybatis.QueryTemplate;
import com.coocaa.ops.common.core.biz.panel.entity.PanelSubPanelRlsEntity;
import com.coocaa.ops.common.tool.base.InstanceUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author bjh
 * @description 板块和子版块关联表-biz层接口实现
 * @date 2020-06-13 11:01:46
 */
@Service
public class  PanelSubPanelRlsBizImpl extends BaseBizImpl<IPanelSubPanelRlsMapper, PanelSubPanelRlsEntity> implements IPanelSubPanelRlsBiz {
    @Override
    public void deleteRls(Integer id, Integer version) {
        QueryTemplate<PanelSubPanelRlsEntity> queryTemplate = this.getPanelSubPanelRlsQuery(id, version);
        if(getMapper().selectCount(queryTemplate) > 0){
            getMapper().delete(queryTemplate);
        }
    }

    @Override
    public void logicDelRls(Integer id, Integer version) {
        QueryTemplate<PanelSubPanelRlsEntity> queryTemplate = this.getPanelSubPanelRlsQuery(id, version);
        PanelSubPanelRlsEntity update = new PanelSubPanelRlsEntity();
        update.setFlag(ResourceStatusEnums.OFF_LINE.getStatus());
        getMapper().update(update, queryTemplate);
    }

    @Override
    public List<Integer> querySubPanelIdList(Integer id, Integer version) {
        QueryTemplate<PanelSubPanelRlsEntity> queryTemplate = this.getPanelSubPanelRlsQuery(id, version);
        List<PanelSubPanelRlsEntity> list =getMapper().selectList(queryTemplate);
        List<Integer> result = InstanceUtil.newArrayList();
        if(CollectionUtils.isEmpty(list)){
            return result;
        }
        for (PanelSubPanelRlsEntity entity : list){
            if(null != entity && null != entity.getSubPanelId()){
                result.add(entity.getSubPanelId());
            }
        }
        return result;
    }


    private QueryTemplate<PanelSubPanelRlsEntity>  getPanelSubPanelRlsQuery(Integer id, Integer version){
        QueryTemplate<PanelSubPanelRlsEntity> queryTemplate = QueryTemplate.getEqQueryTemplate("flag", 1);
        queryTemplate.lambda().eq(PanelSubPanelRlsEntity::getPanelId, id)
                .eq(PanelSubPanelRlsEntity::getPanelVersion, version);
        return queryTemplate;

    }
}
